﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Customer.aspx.cs" Inherits="Shop.Web.CustomerCabinet" %>
<%@ Register TagPrefix="ctrl" TagName="OrdersList" Src="~/Controls/Ajax/OrdersHtmlContent.ascx" %>
<%@ Register TagPrefix="uc" TagName="ListDefaultParams" Src="~/Controls/Ajax/ListDefaultParams.ascx" %>
<%@ Register TagPrefix="ctrl" TagName="AddresList" Src="~/Controls/Ajax/AddresList.ascx" %>
<%@ Register TagPrefix="ctrl" TagName="Addres" Src="~/Controls/Addres.ascx" %>
<asp:Content ID="contentPath" runat="server" ContentPlaceHolderID="pathContent" >
Личный кабинет <asp:Literal runat="server" id="ltPath"></asp:Literal></asp:Content>
<asp:Content ID="contentMain" runat="server" ContentPlaceHolderID="mainContent" >
<asp:DropDownList runat="server" id="ddlCustomers" Visible="false"  Width="300"></asp:DropDownList> <asp:Button runat="server" id="btnChangeCustomer" Text="Изменить пользователя" OnClick="btnChangeCustomer_Click" Visible="False"/><br />
<asp:Literal runat="server" id="ltName"></asp:Literal> <br />
<asp:Literal runat="server" id="ltDiscount"></asp:Literal><br />
<asp:Literal runat="server" id="ltMoney"></asp:Literal><br />
Адреса:<br />
<ctrl:AddresList id="ctrlAddresList" runat="server"></ctrl:AddresList>
<div class="dialog" id="addres" onscroll="SaveAddr()">
    <ctrl:Addres runat="server" ID="adrMain" />
</div>
</div>
<br />
<div id="tabs_sub_menu">
    <ul id="quotesUlMenu">
        <%--<li><a href="#current"><span>Текущиe заказы</span></a></li>
        <li><a href="#preorder"><span>Предзаказ</span></a></li>
        <li><a href="#archiv"><span>Архив заказов</span></a></li>--%>
        <li><a option="2" href="#resultQuotes"><span>Текущие заказы</span></a></li>
        <li><a option="5" href="#resultQuotes"><span>Предзаказ</span></a></li>
        <li><a option="3" href="#resultQuotes"><span>Архив заказов</span></a></li>
        <li><a option="1" href="#resultQuotes"><span>Корзина</span></a></li>
    </ul>
    <div id="resultQuotes">
    </div>
    </div>
<script type="text/javascript">
var DEF_SORT_COL = "OrderReference";
var DEF_SORT_DIR = "False";
<uc:ListDefaultParams runat="server"></uc:ListDefaultParams>

function ChangeDateFormat(id){
	if (id == 1) {
		$("#selDate").removeAttr("disabled");
		$("#txtStartDate").attr("disabled", "disabled");
		$("#txtEndDate").attr("disabled", "disabled");

	} else {
		$("#selDate").attr("disabled", "disabled");
		$("#txtStartDate").removeAttr("disabled");
		$("#txtEndDate").removeAttr("disabled");
	}
	ChangeDate(false);
}


function ChangeDate(load){
	var startDate;
	var endDate;
	if ( $("#rbCI1").attr("checked") == true ) {
		var opt = $("#selDate").get(0).options[$("#selDate").get(0).selectedIndex];
		startDate = new Date(TODAY.getFullYear(), TODAY.getMonth(), TODAY.getDate());
		endDate = new Date(TODAY.getFullYear(), TODAY.getMonth(), TODAY.getDate());
		var offset = opt != 'undefined' && opt.value != 'undefined' ? opt.value : 7;
		switch (offset){
			case "0":
				break;
			case "1":
			case "7":
			case "30":
				startDate.setDate(startDate.getDate() - offset);
				break;
			case "-1":
				startDate.setDate(1);
				break;
			case "-2":
				startDate.setMonth(startDate.getMonth() - 3);
				break;
			case "-3":
				startDate.setFullYear(startDate.getFullYear() - 1);
				break;
		}
		$("#pDates").text(opt.text + " ( " + Date2String(startDate) + " to " + Date2String(TODAY) + " )");
		$("#txtStartDate").val(Date2String(startDate));
		$("#txtEndDate").val(Date2String(TODAY));
	} else {
		startDate = String2Date($("#txtStartDate").val());
		endDate =  String2Date($("#txtEndDate").val());
		$("#pDates").text(Date2String(startDate) + " - " + Date2String(TODAY));
	}
	if (load) {
		CUR_START_DATE = startDate;
		CUR_END_DATE = endDate;
		CUR_PAGE = DEF_PAGE;
		GetCount();
		GetHTML();
	}
}

function GetCount() {
	loadingQuotes = true;
	$.getJSON("AjaxHtmlContent.aspx?Content=OrderItemsCount&creationDateFrom=" + Date2String(CUR_START_DATE) + "&creationDateTo=" + Date2String(new Date(CUR_END_DATE.getFullYear(), CUR_END_DATE.getMonth(), CUR_END_DATE.getDate()+1)), function(json){
		var c = 0;
		var length = json.contact.length;
		for (i = 0; i < length; i++) {
			if (json.contact[i].id == 4 || json.contact[i].id == 6|| json.contact[i].id == 10) {
				c += json.contact[i].value;
			}
		}
		$("#count_my").text("("+c+")");
		c = 0;
		var ca = 0;
		var cc = 0;
		//var cd = 0;
		var ce = 0;
		for (i = 0; i < json.customer.length; i++) {
			if (json.customer[i].id == 4) {
				c += json.customer[i].value;
				ca = json.customer[i].value;
			}
			if (json.customer[i].id == 6) {
				c += json.customer[i].value;
				cc = json.customer[i].value;
			}
//			if (json.customer[i].id == 5) {
//				c += json.customer[i].value;
//				cd = json.customer[i].value;
//			}
			if (json.customer[i].id == 10) {
				c += json.customer[i].value;
				ce = json.customer[i].value;
			}
		}
		$("#count_all").text("("+c+")");
		$("#count_acknowledged").text("("+ca+")");
		$("#count_confirmed").text("("+cc+")");
		//$("#count_despatched").text("("+cd+")");
		$("#count_cancelled").text("("+ce+")");
	});
}
 
function SetTab(val)
{
    CUR_TAB = val;
	CUR_SORT_COL = DEF_SORT_COL;
	CUR_SORT_DIR = DEF_SORT_DIR;
	CUR_PAGE = DEF_PAGE;
	GetHTML();
}

function SetSortCol(val, desc)
{
	CUR_SORT_COL = val;
	CUR_SORT_DIR = desc;
	CUR_PAGE = DEF_PAGE;
	GetHTML();
}

function SetPage(val)
{
	CUR_PAGE = val;
	GetHTML();
}

function SetRowsCount(val)
{
	CUR_ROWS = val;
	GetHTML();
}

var loadingQuotes = false;
function GetHTML() {
	loadingQuotes = true;
	$('#resultQuotes').html("");
	url = 'AjaxHtmlContent.aspx?Content=OrdersList&status=' + CUR_TAB + '&customerId=' + <%= id %>;// + '&sort=' + CUR_SORT_COL + '&desc=' + CUR_SORT_DIR + '&rows=' + CUR_ROWS + '&creationDateFrom=' + Date2String(CUR_START_DATE) + '&creationDateTo=' + Date2String(new Date(CUR_END_DATE.getFullYear(), CUR_END_DATE.getMonth(), CUR_END_DATE.getDate()+1)) + '&page=' + CUR_PAGE; 
	$('#resultQuotes').load(url, {}, 
     function() { 
        //var contentHeight = $('#resultQuotes table').height() + 25;
        //$('#resultQuotes').height(contentHeight);
        //$('#resultQuotes').css("min-height", contentHeight);
     });
     <%--url = 'AjaxHtmlContent.aspx?Content=SendLink';
     $('#sendLink').load(url);--%>
}

function String2Date(str) {
	var d =  new Date();
	var tmp = str.split(/\//);
	d.setFullYear(tmp[2], tmp[1]-1, tmp[0]);
	return d;
}
function Date2String(d) {
	var day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
	var month = d.getMonth() < 9 ? '0' + (d.getMonth()+1) : (d.getMonth()+1) ;
	return day + "/" + month + "/" + d.getFullYear();
}
function SetEndDate(str){
	var sDate = String2Date(str);
	var eDate = $("#txtEndDate").getDatepickerDate();
	if (sDate != 'undefined' && eDate != 'undefined' && sDate > eDate) {
		$("#txtEndDate").setDatepickerDate(sDate);
		$("#txtEndDate").val(Date2String(sDate));
	}
	return sDate;
}

$("#loading").ajaxStart(function(){
	if (loadingQuotes) {
		var count = $("#quotesUlMenu li").length;
		for (i = 1; i <= count; i++) {
			$('#tabs_sub_menu').disableTab(i); 
		}
		$('#resultQuotes *').css("visibility", "hidden");
		$(this).show();
	}
});
$("#loading").ajaxStop(function(){
	if (loadingQuotes) {
		var count = $("#quotesUlMenu li").length;
		for (i = 1; i <= count; i++) {
			$('#tabs_sub_menu').enableTab(i); 
		}
		$(this).hide();
        loadingQuotes = false;
	}
});

<%--$('#tabs_sub_menu').tabs
(
    {
        fxFade: false,
        cache: false,
        remote: false,
        bookmarkable: false
    }
);--%>

$('#tabs_sub_menu').tabs({
    select: function(e, ui) {
            if (!IS_FIRST_LOAD) 
			{
				var option = $(ui.tab).attr("option");
				if (typeof(option) != 'undefined' && option != "")
				{   
					SetTab(option);
                }
			}
    }
});


$(document).ready(function() {
	<%--$("#txtStartDate").datepicker( { firstDay: 1, dateFormat: 'dd/mm/yy', speed: 'fast', showAnim: 'fadeIn' , yearRange: '-5:+5', defaultDate: CUR_START_DATE, mandatory: 'mandatory' }); //
	$("#txtEndDate").datepicker( { firstDay: 1, dateFormat: 'dd/mm/yy', speed: 'fast', showAnim: 'fadeIn' , yearRange: '-5:+5', defaultDate: CUR_END_DATE, mandatory: 'mandatory' });
	$("#txtStartDate").datepicker( { beforeShow: customRange});
    $("#txtStartDate").val(Date2String(CUR_START_DATE));
	$("#txtEndDate").val(Date2String(CUR_END_DATE));
	if (DATE_STARTUP_CHANGE) {
		$("#selDate").attr("disabled", "disabled");
		$("#rbCI2").attr("checked", "checked");
		$("#rbCI1").removeAttr("checked");
	} else {
		$("#txtStartDate").attr("disabled", "disabled");
		$("#txtEndDate").attr("disabled", "disabled");
	}--%>
	if (CUR_TAB != DEF_TAB) {
		var tabs = $("#quotesUlMenu a");
		var count = tabs.length;
		for (i = 1; i <= count; i++) {
			if ($(tabs[i]).attr("option") == CUR_TAB){
				$('#tabs_sub_menu').tabs('select', i);
				break;
			}
		}
	}
	<%--ChangeDate(false);
	GetCount();--%>
	GetHTML();
	IS_FIRST_LOAD = false;
});

// Customize two date pickers to work as a date range 
function customRange(input) { 
    $("#txtEndDate").datepicker( { minDate: String2Date($("#txtStartDate").val())});
}


function showHideObj(str,strImg)
	{
	    var block = "#"+str;
	    var img = "#"+strImg;
	    var obj = document.getElementById(str);
	    var img = document.getElementById(strImg);
	    if (obj.style.display == "none") 
	    {
	        $(block).css("display", "");
	        img.src = "App_Themes/Default/Images/btnMinus.gif";
	    }
	    else 
	    {
	        $(block).css("display", "none");
	        img.src = "App_Themes/Default/Images/btnPlus.gif";
	    }
	}

</script>

</asp:Content>